
<?php
class Application_Controller_Action_Helper_Language extends Zend_Controller_Action_Helper_Abstract {

protected $_sDefaultLanguage;
protected $_aLocales;
protected $_sLanguagesDirectoryPath;

/**
* @param array $aLocales - Available locales
* @param string $sLanguagesDirectoryPath
*/
public function __construct(array $aLocales, $sLanguagesDirectoryPath) {
$this->_sLanguagesDirectoryPath = $sLanguagesDirectoryPath;
$this->_aLocales = $aLocales;
$this->_sDefaultLanguage = key($aLocales); // get first language
}

public function init() {
// try get current language from url
$sLang = $this->getRequest()->getParam('lang');

if(! array_key_exists($sLang, $this->_aLocales)) {
$sLang = $this->_sDefaultLanguage;
}

// generate path to the gettext language file
$sLanguageFilePath = $this->_sLanguagesDirectoryPath . '/'. $sLang . '/LC_MESSAGES/' . $sLang . '.mo';
if(! file_exists($sLanguageFilePath)) {
$sLanguageFilePath = $this->_sLanguagesDirectoryPath . '/' . $this->_sDefaultLanguage . '/LC_MESSAGES/' . $this->_sDefaultLanguage . '.mo';
$sLang = $this->_sDefaultLanguage;
}

// get current locale by current language
$sLocale = $this->_aLocales[$sLang];

// setup translate object
$oTranslate = new Zend_Translate('gettext', $sLanguageFilePath, $sLang);
Zend_Form::setDefaultTranslator($oTranslate); // translated Zend_Form

$this->_actionController->_locale = $sLocale;
$this->_actionController->_lang = $sLang;
$this->_actionController->_translate = $oTranslate;

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->view->locale = $sLocale;
$viewRenderer->view->lang = $sLang;
$viewRenderer->view->translate = $oTranslate;
}
}